package com.juku2024.juku.dialog

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat
import com.juku2024.juku.tools.AppUtils

object DialogUtils {

    fun showAlertDialog(activity: Activity, title: String, msg: String, positiveBtn: String, positive: () -> Unit, negativeButton: String, negative: () -> Unit, ){
        AlertDialog.Builder(activity)
            .setTitle(title)
            .setMessage(msg)
            .setCancelable(false)
            .setPositiveButton(positiveBtn) { _, _ ->
                positive()
            }
            .setNegativeButton(negativeButton) { _, _ ->
                negative()
            }
            .show()
    }

    fun showSetPermissionDialog(activity: Activity, negative: () -> Unit) {
        showAlertDialog(activity, "权限提示", "请前往设置-应用-权限中设置权限，已保证正常使用该功能！",
            "前往", {
                val intent = Intent()
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                intent.data = Uri.fromParts("package", activity.packageName, null)
                activity.startActivity(intent)
            },
            "拒绝", negative
        )
    }

    fun showRequestLocationPermissionDialog(activity: Activity, requestCode: Int, negative: () -> Unit) {
        showAlertDialog(activity, "权限申请", "        为了给你你推荐更多精彩影片和分配更佳流畅的线路，请授予每日影视大全定位权限！ \n        为了正常识别手机设备和运营商网络，保障您的观影体验，请授予每日影视大全读取设备状态权限！ \n " +
                "        为了缓存图片和视频，降低移动流量损耗，请授予每日影视大全读写存储权限！",
            "允许", {
                ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION), requestCode)
            },
            "拒绝", negative
        )
    }

    fun showLoadingDialog(context: Context): CommonLoadingDialog {
        return CommonLoadingDialog.showLoadingDialog(context)
    }

    fun showCommonConfirmDialog(
        context: Context?,
        msg: String,
        btnConfirm: String?,
        onConfirmClickListener: View.OnClickListener?
    ): CommonDialog? {
        if (context == null)
            return null
        val dialog = CommonDialog(context)
        dialog.showDialog(msg, btnConfirm, onConfirmClickListener, null, null)
        val activity = AppUtils.findActivity(context)
        if (activity == null || activity.isFinishing || activity.isDestroyed)
            return null
        dialog.show()
        return dialog
    }

    fun showCommonDialog(
        context: Context?,
        msg: String,
        btnConfirm: String?,
        onConfirmClickListener: View.OnClickListener?,
        btnCancel: String?,
        oncCancelClickListener: View.OnClickListener?
    ): CommonDialog? {
        if (context == null)
            return null
        val dialog = CommonDialog(context)
        dialog.showDialog(msg, btnConfirm, onConfirmClickListener, btnCancel, oncCancelClickListener)
        val activity = AppUtils.findActivity(context)
        if (activity == null || activity.isFinishing)
            return null
        dialog.show()
        return dialog
    }

    fun showCommonDialog(
        context: Context?,
        msg: String,
        btnConfirm: String?,
        onConfirmClickListener: View.OnClickListener?,
        btnCancel: String?,
        oncCancelClickListener: View.OnClickListener?,
        isCancelable: Boolean
    ): CommonDialog? {
        if (context == null)
            return null
        val dialog = CommonDialog(context)
        dialog.setCancelable(isCancelable)
        dialog.showDialog(msg, btnConfirm, onConfirmClickListener, btnCancel, oncCancelClickListener)
        val activity = AppUtils.findActivity(context)
        if (activity == null || activity.isFinishing)
            return null
        dialog.show()
        return dialog
    }

    fun showCommonDialog(
        context: Context?,
        title: String,
        msg: String,
        btnConfirm: String?,
        onConfirmClickListener: View.OnClickListener?,
        btnCancel: String?,
        oncCancelClickListener: View.OnClickListener?,
    ): CommonDialog? {
        if (context == null)
            return null
        val dialog = CommonDialog(context)
        dialog.setCancelable(true)
        dialog.showDialog(title, msg, btnConfirm, onConfirmClickListener, btnCancel, oncCancelClickListener)
        val activity = AppUtils.findActivity(context)
        if (activity == null || activity.isFinishing)
            return null
        dialog.show()
        return dialog
    }

    fun showUpdateDialog(
        context: Context?,
        title: String,
        msg: String,
        btnConfirm: String?,
        onConfirmClickListener: View.OnClickListener?,
        btnCancel: String?,
        oncCancelClickListener: View.OnClickListener?
    ): CommonDialog? {
        if (context == null)
            return null
        val dialog = CommonDialog(context)
        dialog.showDialog(title, msg, btnConfirm, onConfirmClickListener, btnCancel, oncCancelClickListener)
        val activity = AppUtils.findActivity(context)
        if (activity == null || activity.isFinishing)
            return null
        dialog.show()
        return dialog
    }
}